package com.bawei.a2209_mianshi.ui.mall.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.bawei.a2209_mianshi.base.BaseViewModel
import com.bawei.a2209_mianshi.base.state.Default
import com.bawei.a2209_mianshi.base.state.Empty
import com.bawei.a2209_mianshi.base.state.Failed
import com.bawei.a2209_mianshi.base.state.Loading
import com.bawei.a2209_mianshi.base.state.RequestEvent
import com.bawei.a2209_mianshi.base.state.Success
import com.bawei.a2209_mianshi.ui.mall.model.AdInfoModel
import com.bawei.a2209_mianshi.ui.mall.reposiitory.MallRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/12
 */
@HiltViewModel
class MallViewModel @Inject constructor(private val mallRepository: MallRepository):BaseViewModel(){
    private val _mallAdInfoStateFlow  by lazy {
        MutableStateFlow<RequestEvent>(Default)
    }
    val mallAdInfoStateFlow = _mallAdInfoStateFlow.asStateFlow()

    private val _mallAdInfoLiveData by lazy{
        MutableLiveData<AdInfoModel>()
    }
    val mallAdInfoLiveData: LiveData<AdInfoModel> = _mallAdInfoLiveData

    fun cmallAdInfo(){
        execute(
            action = mallRepository.cmallAdInfo(),
            onStart = {
                //开始请求
                _mallAdInfoStateFlow.value = Loading
            },
            onError = {
                //请求失败
                _mallAdInfoStateFlow.value = Failed(it)
            },
            onFinish = {
                //请求结束
                if (it.data == null){
                    _mallAdInfoStateFlow.value = Empty
                }else{
                    _mallAdInfoStateFlow.value = Success

                    _mallAdInfoLiveData.value = it.data
                }

            }
        )
    }
}